//
//  JQFXHShengrAdressaaPicker.swift
//  RongYiHua
//
//  Created by 卢小艳 on 2025/7/12.
//

import Foundation
import UIKit


class JQFXHShengrAdressPickerViewController: UIViewController {
    var jqfxhSelecthhProvince = "北京"
    var jqfxhSelectjjArea = "东城区"
    var jqfxhAddresskkBlock : JQFXHMoliAddressvvBlock?
    var jqfxhMolikkCitys : [JQFXHCityffInfoData] = []
    var jqfxhSelectiiCity = "北京"
    
    @objc func jqfxhCancelBtnqqClick() {
        self.dismiss(animated: true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.jqfxhLoadAddressllData()
        
        self.view.addSubview(jqfxhContentmmView)
        jqfxhContentmmView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(self.view)
            make.top.equalTo(self.view.snp.centerY)
        }
        jqfxhContentmmView.addSubview(jqfxhTitlennLabel)
        jqfxhTitlennLabel.snp.makeConstraints { make in
            make.centerX.equalTo(jqfxhContentmmView)
            make.top.equalTo(jqfxhContentmmView).offset(10)
            make.height.equalTo(32)
        }
        
        jqfxhContentmmView.addSubview(jqfxhCancleooBtn)
        jqfxhCancleooBtn.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(28)
            make.bottom.equalTo(self.view).offset(-UIApplication.shared.jqfxhSafeAreaInsets.bottom-30)
            make.height.equalTo(37)
        }
        
        jqfxhContentmmView.addSubview(jqfxhQueppButton)
        jqfxhQueppButton.snp.makeConstraints { make in
            make.right.equalTo(self.view).offset(-28)
            make.left.equalTo(jqfxhCancleooBtn.snp.right).offset(30)
            make.width.equalTo(jqfxhCancleooBtn)
            make.bottom.height.equalTo(self.jqfxhCancleooBtn)
        }
        

    }
    
    
    
    @objc func jqfxhQueBtnrrClick() {
        let selectedProvinces = pickerView.selectedRow(inComponent: 0)
        self.jqfxhSelecthhProvince = self.jqfxhMolikkCitys[selectedProvinces].nameKey
        
        let selectedCity = pickerView.selectedRow(inComponent: 1)
        self.jqfxhSelectiiCity = self.jqfxhMolikkCitys[selectedProvinces].childrenKey[selectedCity].nameKey
       
        let lastCode = self.jqfxhMolikkCitys[selectedProvinces].childrenKey[selectedCity].codeVaule


        self.jqfxhAddresskkBlock?(jqfxhSelecthhProvince+jqfxhSelectiiCity, lastCode)
        self.dismiss(animated: true)

    }
    
    private func jqfxhLoadAddressllData() {
        
        let provider = JQFXHNetworkManager.shared.shengrProvider
        JQFXHNetworkManager.shared.jqfxhRequest(provider: provider, target: .jqfxhCityssList, type: [JQFXHCityffInfoData].self) { result in
            switch result {
            case.success(let citys):
                self.jqfxhMolikkCitys = citys
                self.jqfxhContentmmView.addSubview(self.pickerView)
                self.pickerView.snp.makeConstraints { make in
                    make.left.right.equalTo(self.jqfxhContentmmView)
                    make.top.equalTo(self.jqfxhTitlennLabel.snp.bottom).offset(10)
                    make.bottom.equalTo(self.jqfxhQueppButton.snp.top).offset(-10)
                }
            case .failure(let error):
                print(error)
            }
        }
        
        

    }
    
    lazy var jqfxhTitlennLabel : UILabel = {
        let navTitleLabel = UILabel(frame: CGRectMake(0, 0, jqfxhScreenWidth, 34))
        navTitleLabel.font = UIFont.jqfxhBold18
        navTitleLabel.textColor = UIColor.black
        navTitleLabel.text = "选择地址"
        return navTitleLabel
    }()
    
    lazy var jqfxhContentmmView : UIView = {
        let jqfxhContentmmView = UIView(frame: CGRect(x: 0, y: jqfxhScreenHeight/2, width: jqfxhScreenWidth, height: jqfxhScreenHeight/2))
        jqfxhContentmmView.backgroundColor = UIColor.white
        return jqfxhContentmmView
    }()
    
    lazy var jqfxhCancleooBtn: UIButton = {
        let nextButton = UIButton(type: .system)
        nextButton.frame = CGRect(x: 30, y: jqfxhScreenHeight/2-UIApplication.shared.jqfxhSafeAreaInsets.bottom-35, width: (jqfxhScreenWidth-90)/2, height: 35)
        nextButton.setTitle("取消", for: .normal)
        nextButton.backgroundColor = UIColor.jqfxhSmallViewBgbbGray
        nextButton.setTitleColor(.lightGray, for: .normal)
        nextButton.layer.cornerRadius = 8
        nextButton.addTarget(self, action: #selector(jqfxhCancelBtnqqClick), for: .touchUpInside)
        return nextButton
    }()
    
    
    
    lazy var pickerView : UIPickerView = {
        let pickerView = UIPickerView(frame: CGRect(x: 0, y: 35, width: jqfxhScreenWidth, height: jqfxhScreenHeight/2-35-UIApplication.shared.jqfxhSafeAreaInsets.bottom-40))
        pickerView.backgroundColor = UIColor.white
        pickerView.dataSource = self
        pickerView.delegate = self
        return pickerView
    }()
    
    
    lazy var jqfxhQueppButton: UIButton = {
        let nextButton = UIButton(type: .system)
        nextButton.frame = CGRect(x: jqfxhScreenWidth/2+15, y: jqfxhScreenHeight/2-UIApplication.shared.jqfxhSafeAreaInsets.bottom-35, width: (jqfxhScreenWidth-90)/2, height: 35)
        nextButton.setTitle("确定", for: .normal)
        nextButton.backgroundColor = UIColor.jqfxhThemexxRed
        nextButton.setTitleColor(.white, for: .normal)
        nextButton.layer.cornerRadius = 8
        nextButton.addTarget(self, action: #selector(jqfxhQueBtnrrClick), for: .touchUpInside)
        return nextButton
    }()
    
}

extension JQFXHShengrAdressPickerViewController : UIPickerViewDataSource {
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if component == 0 {
            // 当选择省份时，重置城市的选择器到默认值（省份对应的第一个城市）
            pickerView.reloadComponent(1) // 重新加载城市组件，以显示新省份的城市列表
            pickerView.selectRow(0, inComponent: 1, animated: true) // 选择默认城市
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            return jqfxhMolikkCitys[row].nameKey
        } else {
            let selectedProvinceIndex = pickerView.selectedRow(inComponent: 0)
            return jqfxhMolikkCitys[selectedProvinceIndex].childrenKey[row].nameKey
        }
    }
    

}


extension JQFXHShengrAdressPickerViewController : UIPickerViewDelegate {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2 // 两个组件：省份和城市
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return jqfxhMolikkCitys.count
        } else {
            let selectedProvinceIndex = pickerView.selectedRow(inComponent: 0)
            return jqfxhMolikkCitys[selectedProvinceIndex].childrenKey.count
        }
    }
}

